{extend name="base"/}
{block name="resources"}
<style>
	.layui-btn:hover {
		opacity: 1;
	}
</style>
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>用户列表展示，admin为管理员用户，不可进行编辑</li>
			{if $auth_cert['type'] != 3}
			{if $auth_cert['user_num'] != 0}
			<li>当前系统授权最大用户数{$auth_cert['user_num'] }(包含管理员用户)</li>
			{else/}
			<li>当前系统授权可创建无限用户</li>
			{/if}
			{else/}
			<li>当前系统授权可创建无限用户，用户可以通过前台系统注册</li>
			{/if}
		</ul>
	</div>
</div>

<!-- 搜索框 -->
<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color" onclick="add()">添加用户</button>

	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="search_keys" placeholder="请输入用户名" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<!-- 列表 -->
<table id="user_list" lay-filter="user_list"></table>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">
		{if $auth_cert['type'] != 3}
		<a class="layui-btn" lay-event="addSite">添加店铺</a>
		{/if}
		{{#  if(d.is_admin == 1){ }}
		<span class="layui-btn ns-cursor ns-text-color-dark-gray">系统用户不可编辑</span>
		{{#  }else{ }}
		<a class="layui-btn" lay-event="detail">查看</a>
		<a class="layui-btn" lay-event="edit">编辑</a>
		<a class="layui-btn" lay-event="delete">删除</a>
		<a class="layui-btn" lay-event="reset_pass">重置密码</a>
		{{#  } }}

	</div>
</script>

<!-- 状态 -->
<script type="text/html" id="status">
	{{ d.status == 1 ? '正常' : '锁定'}}
</script>
{/block}
{block name="script"}
<script>
	var table, form, laytpl, layer_pass, repeat_flag = false; //防重复标识
	layui.use(['form', 'laytpl'], function() {
		form = layui.form;
		laytpl = layui.laytpl;
		form.render();

		table = new Table({
			elem: '#user_list',
			url: ns.url("admin/sysuser/lists"),
			cols: [
				[{
					field: 'username',
					title: '用户名',
					width: '15%',
					unresize: 'false'
				}, {
					field: 'mobile',
					title: '手机号',
					width: '12%',
					unresize: 'false'
				}, {
					field: 'created_site_num',
					title: '已创建站点个数',
					width: '12%',
					unresize: 'false'
				}, {
					field: 'create_time',
					title: '注册时间',
					width: '12%',
					unresize: 'false',
					templet: function(data) {
						return ns.time_to_date(data.create_time);
					}
				},{
					title: '操作',
					width: '25%',
					toolbar: '#operation',
					unresize: 'false'
				}]
			],
		});

		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'addSite':

                    location.href = ns.url("admin/sysuser/addsite?sys_uid=" + data.sys_uid);
                    break;
				case 'detail':
                    location.href = ns.url("admin/sysuser/detail?sys_uid=" + data.sys_uid);
				    break;
				case 'edit': //编辑
					location.href = ns.url("admin/sysuser/edit?sys_uid=" + data.sys_uid);
					break;
				case 'delete': //删除
					deleteUser(data.sys_uid);
					break;
				case 'reset_pass': //重置密码
					resetPassword(data);
					break;
			}
		});

		/**
		 * 删除
		 */
		function deleteUser(sys_uid) {
			if (repeat_flag) return false;
			repeat_flag = true;

			layer.confirm('删除该用户，同时会删除对应的站点，请谨慎操作！', function() {
				$.ajax({
					type: 'POST',
					dataType: 'JSON',
					url: ns.url("admin/sysuser/delete"),
					data: {sys_uid},
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;

						if (res.code == 0) {
							table.reload();
						}
					}
				});
			}, function () {
				layer.close();
				repeat_flag = false;
			});
		}
		
		/**
		 * 重置密码
		 */
		function resetPassword(data) {
			laytpl($("#pass_change").html()).render(data, function(html) {
				layer_pass = layer.open({
					title: '重置密码',
					skin: 'layer-tips-class',
					type: 1,
					area: ['600px'],
					content: html,
				});
			});
		}
		
		form.on('submit(repass)', function(data) {
			if (repeat_flag) return false;
			repeat_flag = true;
			
			$.ajax({
				type: "POST",
				dataType: 'JSON',
				url: ns.url("admin/sysuser/modifyPassword"),
				data: data.field,
				success: function(res) {
					layer.msg(res.message);
					repeat_flag = false;
		
					if (res.code == 0) {
						layer.closeAll('page');
					}
				}
			});
		});
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});
		
		/**
		 * 表单验证
		 */
		form.verify({
			repass: function(value) {
				if (value != $("input[name=password]").val()) {
					return "输入错误,两次密码不一致!";
				}
			}
		});
	});
	
	function add() {
		location.href = ns.url("admin/sysuser/add");
	}
	
	function closePass() {
		layer.close(layer_pass);
	}
</script>

<!-- 重置密码弹框html -->
<script type="text/html" id="pass_change">
	<div class="layui-form" id="reset_pass">
	
		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>新密码：</label>
			<div class="layui-input-block">
				<input type="password" id="new_pass" name="password" lay-verify="required" class="layui-input ns-len-mid" maxlength="18" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);">
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>确认新密码：</label>
			<div class="layui-input-block">
				<input type="password" name="password" lay-verify="required|repass" class="layui-input ns-len-mid" maxlength="18" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);">
			</div>
			<div class="ns-word-aux mid">请再一次输入密码，两次输入密码须一致</div>
		</div>
		
		<div class="ns-form-row mid">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="repass">确定</button>
			<button class="layui-btn layui-btn-primary" onclick="closePass()">返回</button>
		</div>

		<input class="reset-pass-id" type="hidden" name="sys_uid" value="{{d.sys_uid}}"/>
	</div>
</script>
{/block}